//
//  PersonCenterViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/20.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "PersonCenterViewController.h"
#import "headimage.h"
#import "PassValue.h"
@interface PersonCenterViewController ()<UITableViewDataSource,UITableViewDelegate>

@interface PersonCenterViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property(nonatomic,strong)UIButton *btnimage;
@property(nonatomic,strong)UIImageView *imageview;
@property(nonatomic,strong)UITextField *textgender;
@property(nonatomic,strong)UITextField *textname;
@property(nonatomic,strong)UITextField *textemail;
@property(nonatomic,strong)UITextField *textphone;
@property(nonatomic,strong)UITextField *textpath;

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray *array;
@property(nonatomic,assign)BOOL canedit;
 

@end

@implementation PersonCenterViewController
-(NSArray *)array{
    if (!_array) {
        _array = [NSArray array];
    }return _array;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _tableview.delegate = self;
        _tableview.dataSource =self;
    }return _tableview;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.imagePicker = [[UIImagePickerController alloc]init];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.canedit = NO;
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    imageview.image=[UIImage imageNamed:@"手机.jpg"];
    [self.tableview addSubview:imageview];
    imageview.alpha = 0.5;
    
    
    
    self.navigationItem.title =@"资料设置";
    [self.view addSubview:self.tableview];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightbtnaction)];
 
 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
//                self.imageview.backgroundColor = [UIColor greenColor];
                self.imageview.layer.cornerRadius = 50;
                self.imageview.layer.masksToBounds=YES;
                self.imageview.image = self.image1;
                self.btnimage = [[UIButton alloc]initWithFrame:CGRectMake(kWidth-100, 25, 100, 50)];
                [self.btnimage  setTitle:@"跟换头像" forState:UIControlStateNormal];
                [self.btnimage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [self.btnimage addTarget:self action:@selector(btnimagecation) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:self.btnimage];
                [cell addSubview:self.imageview];
                
                
                break;
            case 1:
                self.textname =[[UITextField alloc]initWithFrame:CGRectMake(kWidth/2, 0, kWidth/2-10, 44)];
                if (![[PassValue sharePassValue] username]) {
                    self.textname.text =@"";
                } else {
                    self.textname.text = [[PassValue sharePassValue] username];
                }
                self.textname.textAlignment =NSTextAlignmentRight;
                self.textname.userInteractionEnabled = NO;
                [cell addSubview:self.textname];
                cell.textLabel.text = @"昵称";
                
                break;
            case 2:
                self.textgender=[[UITextField alloc]initWithFrame:CGRectMake(kWidth/2, 0, kWidth/2-10, 44)];
                if (![[PassValue sharePassValue] gender]) {
                    self.textgender.text = @"";
                } else {
                    self.textgender.text = [[PassValue sharePassValue] gender];
                }

                self.textgender.textAlignment =NSTextAlignmentRight;
                self.textgender.userInteractionEnabled = NO;
                [cell addSubview:self.textgender];
                cell.textLabel.text = @"性别";
                break;
            case 3:
                self.textemail = [[UITextField alloc]initWithFrame:CGRectMake(kWidth/2, 0, kWidth/2-10, 44)];
                self.textemail.text =@"";
                self.textemail.textAlignment =NSTextAlignmentRight;
                self.textemail.userInteractionEnabled = NO;
                [cell addSubview:self.textemail];
                cell.textLabel.text = @"邮箱";
                break;
            case 4:
                self.textphone =[[UITextField alloc]initWithFrame:CGRectMake(kWidth/2, 0, kWidth/2-10, 44)];
                self.textphone.text =@"";
                self.textphone.textAlignment =NSTextAlignmentRight;
                self.textphone.userInteractionEnabled = NO;
                [cell addSubview:self.textphone];
                cell.textLabel.text = @"电话号码";
                break;
                
            default:
                break;
        }
    }
    if (indexPath.section ==1) {
        switch (indexPath.row) {
            case 0:
                self.textpath = [[UITextField alloc]initWithFrame:CGRectMake(kWidth/2, 0, kWidth/2-10, 44)];
                if (![[PassValue sharePassValue] city]) {
                    self.textpath.text=@"";
                } else {
                    self.textpath.text = [[PassValue sharePassValue] city];
                }
                self.textpath.textAlignment = NSTextAlignmentRight;
                self.textpath.userInteractionEnabled = NO;
                [cell addSubview:self.textpath];
                cell.textLabel.text =@"地区";
                break;
            case 1:
                cell.textLabel.text = @"";
                break;
                
            default:
                break;
        }
    }
    
    
    return cell;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (section ==0) {
//        return 10;
//    }
//    return 20;
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section ==0){
        return 5;
    }
    return 2;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0&&indexPath.row==0) {
        return 100;
    }
    return 44;
    
}










-(void)rightbtnaction{
 
    if (self.canedit == NO) {
        self.navigationItem.rightBarButtonItem.title=@"确定";
        self.textname.userInteractionEnabled = YES;
        self.textname.borderStyle = UITextBorderStyleRoundedRect;
        self.textgender.userInteractionEnabled = YES;
        self.textgender.borderStyle = UITextBorderStyleRoundedRect;
        self.textemail.userInteractionEnabled =YES;
        self.textemail.borderStyle = UITextBorderStyleRoundedRect;
        self.textphone.userInteractionEnabled = YES;
        self.textphone.borderStyle = UITextBorderStyleRoundedRect;
        self.textpath.userInteractionEnabled = YES;
        self.textpath.borderStyle = UITextBorderStyleRoundedRect;
        self.canedit =YES;
    }else{
        self.navigationItem.rightBarButtonItem.title=@"编辑";
        self.textname.userInteractionEnabled = NO;
        self.textname.borderStyle = UITextBorderStyleNone;
        self.textgender.userInteractionEnabled = NO;
        self.textgender.borderStyle = UITextBorderStyleNone;
        self.textemail.userInteractionEnabled = NO;
        self.textemail.borderStyle = UITextBorderStyleNone;
        self.textphone.userInteractionEnabled = NO;
        self.textphone.borderStyle = UITextBorderStyleNone;
        self.textpath.userInteractionEnabled = NO;
        self.textpath.borderStyle = UITextBorderStyleNone;
        self.canedit =NO;
    }
}
//更换头像方法
-(void)btnimagecation{
    
//    headimage *headvc =[[headimage alloc]init];
//    NSLog(@"123");
//    headvc.image = self.imageview.image;
//    
//    __weak typeof(self) weakself = self;//或者是这样写
//    headvc.block = ^(UIImage *aimage){
//        
//        weakself.imageview.image = aimage;
//    };
//    [self.navigationController pushViewController:headvc animated:YES];
    
    UIAlertController *  alert = [UIAlertController alertControllerWithTitle:@"" message:@"系统消息" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //添加取消按钮
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:actionCancel];
    //添加按钮
    UIAlertAction *pickPhoto = [UIAlertAction actionWithTitle:@"调用相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            _imagePicker.delegate = self;
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
    [alert addAction:pickPhoto];
    
    //添加按钮
    UIAlertAction *takePhoto = [UIAlertAction actionWithTitle:@"调用相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            _imagePicker.delegate = self;
            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            
            
            [self presentViewController:_imagePicker animated:YES completion:nil];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请使用真机调用相机" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                [self performSelector:@selector(removeAlertView:) withObject:alert afterDelay:0.8];
            }];
        }
    }];
    [alert addAction:takePhoto];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

////调用系统相片
//-(void)replaceimage:(id)sender{
//    //底部提示框
//    UIAlertController *  alert = [UIAlertController alertControllerWithTitle:@"" message:@"系统消息" preferredStyle:UIAlertControllerStyleActionSheet];
//    
//    //添加取消按钮
//    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//    [alert addAction:actionCancel];
//    //添加按钮
//    UIAlertAction *pickPhoto = [UIAlertAction actionWithTitle:@"调用相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
//        {
//            _imagePicker.delegate = self;
//            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            [self presentViewController:self.imagePicker animated:YES completion:nil];
//        }
//    }];
//    [alert addAction:pickPhoto];
//    
//    //添加按钮
//    UIAlertAction *takePhoto = [UIAlertAction actionWithTitle:@"调用相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//            _imagePicker.delegate = self;
//            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//            
//            
//            
//            [self presentViewController:_imagePicker animated:YES completion:nil];
//        }else{
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请使用真机调用相机" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//            [self presentViewController:alert animated:YES completion:^{
//                [self performSelector:@selector(removeAlertView:) withObject:alert afterDelay:0.8];
//            }];
//        }
//    }];
//    [alert addAction:takePhoto];
//    
//    
//    [self presentViewController:alert animated:YES completion:nil];
//    
//    
//}
- (void)removeAlertView:(UIAlertController*)alert
{
    //alertView消失
    [alert dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *sourceImage = info[UIImagePickerControllerOriginalImage];
    self.imageview.image = sourceImage;
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
